#include <QComboBox>
#include <QString>
#include <QDebug>
#include <QApplication>
#include <QPainter>
#include <QImage>
#include "prioritydelegate.h"
#include "Task.h"

QWidget* PriorityDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                      const QModelIndex &index) const {

    QComboBox* box = new QComboBox(parent);

    box->insertItem(Task::LOW, "Low");
    box->insertItem(Task::MEDIUM, "Medium");
    box->insertItem(Task::HIGH, "High");

    return box;
}

void PriorityDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {

    QComboBox* box = static_cast<QComboBox*>(editor);
    int model_index = index.model()->data(index).toInt();
    box->setCurrentIndex(model_index);
}

void PriorityDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                  const QModelIndex &index) const {

    QComboBox* box = static_cast<QComboBox*>(editor);
    model->setData(index, box->currentIndex());
}

void PriorityDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const {

    editor->setGeometry(option.rect);
}

QSize PriorityDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
    if (index.column() != PRIORITY) {
        return QStyledItemDelegate::sizeHint(option, index);
    }

    //QRect rect = QImage("img/1.ico").rect();
    //return QSize(rect.width(), rect.height());
    return QSize(10, 100);
}

void PriorityDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                             const QModelIndex &index) const {
    if (index.column() != PRIORITY) {
        QStyledItemDelegate::paint(painter, option, index);
        return;
    }

    int priority = index.model()->data(index, Qt::DisplayRole).toInt();
    QString path = "img/" + QString::number(priority + 1) + ".ico";
    painter->drawImage(option.rect, QImage(path));
}
